package com.liliudong.template.api.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.liliudong.template.biz.service.sys.SysRoleService;
import com.liliudong.template.common.R;
import com.liliudong.template.common.convert.sys.SysRoleConvert;
import com.liliudong.template.common.dto.sys.SysRoleAddOrUpdateDTO;
import com.liliudong.template.common.entity.sys.SysRole;
import com.liliudong.template.common.vo.PageVO;
import com.liliudong.template.common.vo.sys.SysRoleBaseVO;
import com.liliudong.template.common.vo.sys.SysRoleDetailVO;
import com.liliudong.template.common.vo.sys.SysRolePageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色
 *
 * @author ldli5
 * @classname SysRoleController
 * @date 2024/6/20-13:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/sys/role")
public class SysRoleController {

	private final SysRoleService sysRoleService;

	/**
	 * 添加一个
	 *
	 * @param sysRoleAddOrUpdateDTO sys角色添加或更新dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:role:add")
	@PostMapping
	public R<?> addOne(@RequestBody @Validated SysRoleAddOrUpdateDTO sysRoleAddOrUpdateDTO) {
		sysRoleService.addOne(sysRoleAddOrUpdateDTO);
		return R.ok();
	}

	/**
	 * 删除一个
	 *
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:role:delete")
	@DeleteMapping("/{id}")
	public R<?> deleteOne(@PathVariable Long id) {
		sysRoleService.deleteById(id);
		return R.ok();
	}

	/**
	 * 更新一个
	 *
	 * @param sysRoleAddOrUpdateDTO sys角色添加或更新dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:role:modify")
	@PutMapping("/{id}")
	public R<?> modifyById(@PathVariable Long id, @RequestBody @Validated SysRoleAddOrUpdateDTO sysRoleAddOrUpdateDTO) {
		sysRoleService.modifyById(id, sysRoleAddOrUpdateDTO);
		return R.ok();
	}

	/**
	 * 分页查询
	 *
	 * @param current current
	 * @param size    大小
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:role:list")
	@GetMapping("/page")
	public R<PageVO<SysRolePageVO>> page(@RequestParam(defaultValue = "1") Long current,
	                                     @RequestParam(defaultValue = "10") Long size) {
		PageVO<SysRolePageVO> sysRolePage = sysRoleService.queryPage(current, size);
		return R.ok(sysRolePage);
	}

	/**
	 * 全部
	 *
	 * @return {@link R }<{@link List }<{@link SysRole }>>
	 */
	@SaCheckPermission("sys:role:list")
	@GetMapping("/all")
	public R<List<SysRoleBaseVO>> all() {
		List<SysRole> sysRoleList = sysRoleService.list();
		return R.ok(SysRoleConvert.INSTANCE.entity2BaseVo(sysRoleList));
	}

	/**
	 * id明细
	 *
	 * @param id 身份证件
	 * @return {@link R }<{@link SysRoleDetailVO }>
	 */
	@SaCheckPermission("sys:role:detail")
	@GetMapping("/detail/{id}")
	public R<SysRoleDetailVO> detailById(@PathVariable Long id) {
		SysRoleDetailVO sysRoleDetailVO = sysRoleService.detailById(id);
		return R.ok(sysRoleDetailVO);
	}
}
